package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Category;
import com.itheima.entity.Movies;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findList() {
        return categoryMapper.findList();
    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, Integer cid, String keyword) {
        //1.参数校验
        if (pageNum<=0) {
            pageNum=1;
        }
        if (pageSize<=0) {
            pageSize=5;
        }

        PageHelper.startPage(pageNum,pageSize);
        keyword = (keyword==null?null:keyword.trim());
        List<Category> list = categoryMapper.findList2(cid,keyword);

        //- 使用PageInfo构造器封装list,就可以获取总记录数和分页的一些其他参数
        return new PageInfo(list);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
         categoryMapper.deleteBatch(ids);
    }

    @Override
    public Category getByid(String id) {
        return categoryMapper.getByid(id);
    }

    @Override
    public void saveOrUpdate(Category category) {
        //有id的时候更新,没有id的时候添加
        if (category.getId() != null)
        {
           categoryMapper.updateById(category);
        }else
        {
            categoryMapper.insert(category);
        }
    }

    @Override
    public void deleteById(String id) {
        categoryMapper.deleteById(id);
    }
}
